package action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.News;

import dao.NewsDao;

public class NewsServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String page = req.getParameter("page");
		NewsDao nd = new NewsDao();
		ArrayList<News> news_list = nd.getAllNews();
		ArrayList<News> news_list1 = new ArrayList<News>();
		for (int i = 0; i < news_list.size() && i < 12; i++) {
			news_list1.add(news_list.get(i));
		}

		HttpSession session = req.getSession();
		session.setAttribute("news_list", news_list);
		session.setAttribute("news_list1", news_list1);

		session.setAttribute("news_total_items", news_list.size());
		int num = news_list.size() % 25;
		int news_total_pages = (num == 0 ? news_list.size() / 25 : news_list
				.size() / 25 + 1);
		session.setAttribute("news_total_pages", news_total_pages);
		session.setAttribute("news_current_page", 1);

		resp.sendRedirect("separateNews?page=" + page);
	}
}
